package com.qiguliuxing.dts.job.config;

import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.qiguliuxing.dts.job.listener.JobListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 判断是否配置了zookeeper 地址
 *  job集群注册中心配置
 * @author qiguliuxing
 * @since 1.0.0
 */
@Configuration
@ConditionalOnExpression("'${zookeeper.address}'.length() > 0")
public class ElasticRegCenterConfig {
   
	/**
     * 配置zookeeper
     * @param serverList
     * @param namespace
     * @return
     */
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter regCenter(
            @Value("${zookeeper.address}") final String serverList,
            @Value("${regCenter.namespace}") final String namespace) {
        return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
    }

    /**
     * 配置任务监听器
     * @return
     */
    @Bean
    public ElasticJobListener jobListener() {
        return new JobListener();
    }
}
